#include "comm/my_defs.h"

void test_bool()
{
    my_bool_t b = MY_TRUE;
    assert(b);

    b = MY_FALSE;
    assert(!b);

    my_result_t r = MY_SUCCESS;
    assert(r);

    r = MY_FAILED;
    assert(!r);
}

void test_abs_and_swap()
{
    assert(MY_ABS(100) == 100 && MY_ABS(-100) == 100);

    int a = 10;
    int b = 20;
    int tmp;
    MY_SWAP(a, b, tmp);
    assert(a == 20 && b == 10);
}

void test_max_and_min()
{
    assert(MY_MAX(1, 10) == 10);

    assert(MY_MIN(1, 100) == 1);
}

void test_align()
{
    assert(MY_ALIGN(0, MY_PAGE_SIZE) == 0);
    assert(MY_ALIGN(1, MY_PAGE_SIZE) == 4096);
    assert(MY_ALIGN(4095, MY_PAGE_SIZE) == 4096);
    assert(MY_ALIGN(4096, MY_PAGE_SIZE) == 4096);
    assert(MY_ALIGN(4097, MY_PAGE_SIZE) == 8192);
    assert(MY_ALIGN(8190, MY_PAGE_SIZE) == 8192);
    assert(MY_ALIGN(8192, MY_PAGE_SIZE) == 8192);
    assert(MY_ALIGN(8193, MY_PAGE_SIZE) == 12288);

    assert(MY_ALIGN_PTR(0, MY_PAGE_SIZE) == (u_char *)0);
    assert(MY_ALIGN_PTR(1, MY_PAGE_SIZE) == (u_char *)4096);
    assert(MY_ALIGN_PTR(4095, MY_PAGE_SIZE) == (u_char *)4096);
    assert(MY_ALIGN_PTR(4097, MY_PAGE_SIZE) == (u_char *)8192);
}

typedef struct {
    uint64_t        a:38;
} my_test_type_t;

void test_castbit()
{
    uint64_t a;

    a = 123456789;  // < 2^38 - 1
    my_test_type_t t = {
        .a = MY_CASTBIT(a, 38)
    };
    assert(t.a == a);

    a = 111111111111; // < 2^38 - 1
    t.a = MY_CASTBIT(a, 38);
    assert(t.a == a);

    a = 222222222222; // < 2^38 - 1
    t.a = MY_CASTBIT(a, 38);
    assert(t.a == a);
}


int main()
{
    test_bool();
    test_abs_and_swap();
    test_max_and_min();
    test_align();
    test_castbit();

    return 0;
}